home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / dialog-0.000 / dialog-0 / dialog-0.6c / yesno.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-17  |  3.0 KB  |  116 lines

  1. /*
  2.  *  yesno.c -- implements the yes/no box
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "dialog.h"
  22.  
  23. /*
  24.  * Display a dialog box with two buttons - Yes and No
  25.  */
  26. int
  27. dialog_yesno (const char *title, const char *prompt, int height, int width)
  28. {
  29.     int i, x, y, key = 0, button = 0;
  30.     WINDOW *dialog;
  31.  
  32.     /* center dialog box on screen */
  33.     x = (COLS - width) / 2;
  34.     y = (LINES - height) / 2;
  35.  
  36. #ifdef HAVE_NCURSES
  37.     if (use_shadow)
  38.     draw_shadow (stdscr, y, x, height, width);
  39. #endif
  40.     dialog = newwin (height, width, y, x);
  41.     mouse_setbase (x, y);
  42.     keypad (dialog, TRUE);
  43.  
  44.     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  45.     wattrset (dialog, border_attr);
  46.     wmove (dialog, height - 3, 0);
  47.     waddch (dialog, ACS_LTEE);
  48.     for (i = 0; i < width - 2; i++)
  49.     waddch (dialog, ACS_HLINE);
  50.     wattrset (dialog, dialog_attr);
  51.     waddch (dialog, ACS_RTEE);
  52.     wmove (dialog, height - 2, 1);
  53.     for (i = 0; i < width - 2; i++)
  54.     waddch (dialog, ' ');
  55.  
  56.     if (title != NULL) {
  57.     wattrset (dialog, title_attr);
  58.     wmove (dialog, 0, (width - strlen (title)) / 2 - 1);
  59.     waddch (dialog, ' ');
  60.     waddstr (dialog, title);
  61.     waddch (dialog, ' ');
  62.     }
  63.     wattrset (dialog, dialog_attr);
  64.     print_autowrap (dialog, prompt, width - 2, 1, 3);
  65.  
  66.     x = width / 2 - 10;
  67.     y = height - 2;
  68.     print_button (dialog, "  No  ", y, x + 13, FALSE);
  69.     print_button (dialog, " Yes ", y, x, TRUE);
  70.     wrefresh (dialog);
  71.  
  72.     while (key != ESC) {
  73.     key = mouse_wgetch (dialog);
  74.     switch (key) {
  75.     case 'Y':
  76.     case 'y':
  77.         delwin (dialog);
  78.         return 0;
  79.     case 'N':
  80.     case 'n':
  81.         delwin (dialog);
  82.         return 1;
  83.  
  84.     case M_EVENT + 'y':    /* mouse enter... */
  85.     case M_EVENT + 'n':    /* use the code for toggling */
  86.         button = (key == M_EVENT + 'y');
  87.  
  88.     case TAB:
  89.     case KEY_UP:
  90.     case KEY_DOWN:
  91.     case KEY_LEFT:
  92.     case KEY_RIGHT:
  93.         if (!button) {
  94.         button = 1;    /* "No" button selected */
  95.         print_button (dialog, " Yes ", y, x, FALSE);
  96.         print_button (dialog, "  No  ", y, x + 13, TRUE);
  97.         } else {
  98.         button = 0;    /* "Yes" button selected */
  99.         print_button (dialog, "  No  ", y, x + 13, FALSE);
  100.         print_button (dialog, " Yes ", y, x, TRUE);
  101.         }
  102.         wrefresh (dialog);
  103.         break;
  104.     case ' ':
  105.     case '\n':
  106.         delwin (dialog);
  107.         return button;
  108.     case ESC:
  109.         break;
  110.     }
  111.     }
  112.  
  113.     delwin (dialog);
  114.     return -1;            /* ESC pressed */
  115. }
  116.